package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.List;

/**
 * 线下仿写
 */
public class FangXieAdapter  extends RecyclerView.Adapter<FangXieAdapter.MyViewHolder> {
    Context context;
    private List<TIbean> beans;

    public FangXieAdapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.eweitingtext, parent, false);
        FangXieAdapter.MyViewHolder viewHolder = new FangXieAdapter.MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.timu_num.setText(position + 1 + "");
        int i = position;
        for (int j = 0; j < beans.get(position).getContent().size(); j++) {
            questionContentAdd(holder.questionLine, beans.get(position).getContent().get(j), beans.get(position).getDt_type(),position);
        }
        for (int j = 0; j < beans.get(i).getAnswers().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(i).getOption().get(j).getOptions(), holder.questionLine);
        }
    }
    /**
     * 内容
     * @param questionLine
     * @param string
     */
    private void questionContentAdd(final FlowLayout questionLine, String string, final String type,final int position) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
//        params.setMargins(0, 5, 0, 5);
        if ("_".equals(string)) {
            TextView textView = new TextView(context);
            textView.setText(string + "__");
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setTag(R.id.selected, 0);
            textView.setPadding(15, 0, 15, 0);
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        } else {
            TextView textView = new TextView(context);
            textView.setText(string);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        }
    }
    /**
     * 答案
     * @param answerLine
     * @param string
     * @param questionLine
     */
    private void answerLineContentAdd(FlowLayout answerLine, String string, final FlowLayout questionLine) {
        // 设置添加的textvIew的参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 25, 5);

        TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(16);
        textView.setTextColor(Color.parseColor("#ffffff"));
        textView.setLayoutParams(params);
        textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        answerLine.addView(textView);
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }


    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView timu_num;
        public FlowLayout questionLine;
        public FlowLayout answerLine;

        public MyViewHolder(View itemView) {
            super(itemView);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
            questionLine = (FlowLayout) itemView.findViewById(R.id.questionLine);
            answerLine = (FlowLayout) itemView.findViewById(R.id.answerLine);
        }
    }


}
